import cv2
import time,PIL,os
from PIL import ImageGrab
import numpy as np
import time
from tempfile import NamedTemporaryFile
import cv2
import easyocr
from pprint import pprint
from concurrent.futures import ThreadPoolExecutor
import sys

dataDir = "D:/argostranslate/data"
cacheDir = "D:/argostranslate/cache"
os.environ["XDG_DATA_HOME"] = dataDir
os.environ["XDG_CACHE_HOME"] = cacheDir
import argostranslate.package
import argostranslate.translate
#截图
class ScreenCut:
    def __init__(self):
        if not os.path.exists("E:/ningyuan/sumimoto/Curelasto/tmp/"):
            os.makedirs("E:/ningyuan/sumimoto/Curelasto/tmp/")
        try:
            os.remove("E:/ningyuan/sumimoto/Curelasto/tmp/scr.png")
            os.remove("E:/ningyuan/sumimoto/Curelasto/tmp/cut.png")
        except:pass
        self.scrImg = open("E:/ningyuan/sumimoto/Curelasto/tmp/scr.png","wb+")
        self.cutImg = open("E:/ningyuan/sumimoto/Curelasto/tmp/cut.png","wb+")
    def cut(self):
        self.scrren_cut()
        #print("截图完成")
    def scrren_cut(self):
         # 截取固定区域 (500,500) 到 (600,600)
         # 获取传递给Python脚本的参数
        arg1 = sys.argv[1]
        arg2 = sys.argv[2]
        # 打印参数，并返回结果
        #print("Argument 1 is:", arg1)
        #print("Argument 2 is:", arg2)

        x = int(arg1)  # 将第一个参数转换为整数
        y = int(arg2)  # 将第二个参数转换为整数
        #print("x is:", x)
        #print("y is:", y)
        # img = ImageGrab.grab(bbox=(
        #         1000,       # 起始x坐标
        #         800,       # 起始y坐标
        #         1000+500,  # 起始x + 宽度
        #         800+200   # 起始y + 高度
        #     ))
        img = ImageGrab.grab(bbox=(
                x,       # 起始x坐标
                y,       # 起始y坐标
                x+500,  # 起始x + 宽度
                y+200   # 起始y + 高度
            ))
        img.save(self.cutImg.name)


cut = ScreenCut()
#print("Init done.")

cut.cut()
#print("cut done.")

# Create an OCR reader object
reader = easyocr.Reader(['en'])
# Read text from an image
result = reader.readtext("E:/ningyuan/sumimoto/Curelasto/tmp/cut.png")

if not result:
    #print("未识别到文字")
    print("Text not recognized")
    exit()

#print("result>>>>>>>>>>>",result)

#extracted_text = []
extracted_text = ''
for detection in result:
    #extracted_text.append(detection[1])
    #print(detection[1])
    extracted_text += detection[1]


#print("111111111111111111111111111")
print(extracted_text)